﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace Omabar.Common
{
    public static class Util
    {
        public static string GetUID()
        {
            DateTime now = DateTime.Now;
            try
            {
                string id = now.ToString("yyyyMMddHHmmssfff");
                return id;
            }
            catch (Exception ex)
            {
                long year = now.Year;
                long month = now.Month;
                long day = now.Day;
                long hour = now.Hour;
                long minute = now.Minute;
                long second = now.Second;
                long millisecond = now.Millisecond;

                long uid = year * 10000000000000 + month * 100000000000 + day * 1000000000 + hour * 10000000 + minute * 100000 + second * 1000 + millisecond;
                return uid + "";
            }
        }

        public static string GetTemplateFileContent(string templateFilePath)
        {
            Encoding code = Encoding.GetEncoding("utf-8");
            // 读取模板文件
            StreamReader sr = null;
            string str = string.Empty;
            try
            {
                sr = new StreamReader(templateFilePath, code);
                str = sr.ReadToEnd();
            }
            catch (Exception ex)
            {
                sr.Close();
                Tom.Net.Log.LogManager.Error("读取模版错误", ex);
            }

            return str;
        }

        public static bool GenerateStaticHTML(string replacedContent, string path)
        {
            Encoding code = Encoding.GetEncoding("utf-8");
            StreamWriter sw = null;
            try
            {
                sw = new StreamWriter(path, false, code);
                sw.Write(replacedContent);
                sw.Flush();
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("生成静态文件错误", ex);
            }
            finally
            {
                sw.Close();
            }
            return true;
        }
    }
}
